package indi.gao.redis;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>Description:</p>
 * <p>Copyright: Copyright (c)2019$</p>
 * <p>Company: Tope</p>
 * <P>Created Date :2019$-08$-09$</P>
 * <P>@version 1.0</P>
 */
public class txtTrans {


    public static void main(String[] args) throws IOException {
        List<String> studentIDsList = new ArrayList<>();
        String result = "";
        /* 读取数据 */

        BufferedReader bookNames = new BufferedReader(new InputStreamReader(new FileInputStream(new File("TxtReadAndWrite/src/main/file/oldSql.txt")), "UTF-8"));
        BufferedReader bookNames1 = new BufferedReader(new InputStreamReader(new FileInputStream(new File("TxtReadAndWrite/src/main/file/oldSqlResult.txt")), "UTF-8"));
        String lineTxt = null;

        List<Student> objects = new ArrayList<>();
        List<Person> persons = new ArrayList<>();
        while ((lineTxt = bookNames.readLine()) != null) {//读取每行的数据
            String[] split = lineTxt.split("\t");
            String orgCode = split[0];
            String orgName = split[2];
            String orgId = split[3];
            String tel = split[4];
            String province = split[5];
            String city = split[6];
            String AreaName = split[7];
            String address = split[8];
            String parentId = split[9];

            Student student = new Student();
            student.setOrgCode(orgCode);
            student.setOrgName(orgName);
            student.setOrgId(orgId);
            student.setTel(tel);
            student.setProvince(province);
            student.setCity(city);
            student.setAreaName(AreaName);
            student.setAddress(address);
            student.setParentId(parentId);
            objects.add(student);
        }
        while ((lineTxt = bookNames1.readLine()) != null) {//读取每行的数据
            String[] split = lineTxt.split("\t");
            String personCode = split[0];
            String name = split[1];
            String orgId = split[5];
            String mobile = split[6];

            Person person = new Person();
            person.setOrgId(orgId);
            person.setPersonCode(personCode);
            person.name = name;
            person.mobile = mobile;
            persons.add(person);
        }
        ArrayList<SyncStudent> objects1 = new ArrayList<>();
        for (Student object : objects) {
            SyncStudent syncStudent = new SyncStudent();
            syncStudent.setOrgCode(object.getOrgCode());
            syncStudent.setOrgName(object.getOrgName());

            List<Student> collect = objects.stream().filter(x -> x.getOrgId().equals(object.getParentId())).collect(Collectors.toList());
            if (collect.size() > 0) {
                syncStudent.setParentOrgName(collect.get(0).getOrgName());
                syncStudent.setParentOrgCode(collect.get(0).getOrgCode());
            }
            syncStudent.setContactPersonPhone(object.getTel());
            syncStudent.setAddressDetail(object.getAddress());
            syncStudent.setAddressArea(object.getProvince() + "," + object.getCity() + "," + object.getAreaName());
            objects1.add(syncStudent);
        }
//        System.out.println(JSONObject.toJSONString(objects1));

        for (Person person : persons) {
            List<Student> collect = objects.stream().filter(x -> x.getOrgId().equals(person.getOrgId())).collect(Collectors.toList());
            if (!collect.isEmpty()) {
                person.setReOrgCode(collect.get(0).getOrgCode());
            }
        }
        System.out.println(JSONObject.toJSONString(persons));
    }

    @Data
    static class Student {
        String orgCode;
        String orgName;
        String orgId;
        String tel;
        String province;
        String city;
        String AreaName;
        String address;
        String parentId;
    }

    @Data
    static class Person {
        String personCode;
        String name;
        String orgId;
        String reOrgCode;
        String mobile;
        String companyId = "538640581825339696495112";
        Boolean isDelete = false;
        String identifyType = "channelPerson";
    }

    @Data
    static class SyncStudent {
        private String orgCode;

        private String orgName;

        private String orgTypeName = "经销商";

        private String parentOrgCode;

        private String parentOrgName;

        private String contactPersonName;
        private String contactPersonPhone;

        private String addressArea;

        private String addressDetail;

        private String companyId = "538640581825339696495112";

        private Boolean isNew = false;
    }

}
